package com.bigshen.algorithm.gTree.solution01SerializableDes;

import java.util.ArrayList;
import java.util.List;

public class Codec {

    class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        public TreeNode(int val) {
            this.val = val;
        }
    }

    // Encodes a tree to a single string.
    public String serialize(TreeNode root) {

        return null;
    }

    // {1,2,3,#,#,4,5}
    // Decodes your encoded data to tree.
    public TreeNode deserialize(String data) {

        if (null == data) {
            return null;
        }
        String[] array = data.replace("{", "").replace("}", "").split(",");
        TreeNode[] nodes = new TreeNode[array.length];
        TreeNode root = new TreeNode(Integer.valueOf(array[0]));
        nodes[0] = root;
        for (int i = 1; i < array.length; i++) {
            if (array[i].equals("#")) {
                continue;
            }
            TreeNode child = new TreeNode(Integer.valueOf(array[i]));
            int j = i+1;
            if (j%2 == 0) {
                // left
                nodes[(i-1)/2].left = child;
            } else {
                // right
                nodes[(i-1)/2].right = child;
            }
            nodes[i] = child;
        }
        return root;
    }

    public static void main(String[] args) {

        String data = "{1,2,3,#,#,4,5}";

//        TreeNode root = new Codec().deserialize(data);
//        System.out.println(root);

        System.out.println("0123".substring(1, 4));

        String[] dataArray = data.substring(1, data.length()-1).split(",");

    }

}

